<template>
  <div class="app-container">
    <div class="tech-border-out">
      <div class="tech-border">
        <el-card>
          <div slot="header" class="clearfix">
              <dv-decoration-7 style="width:150px;height:30px;"> <p style="padding: 0 5px;">账号管理</p>  </dv-decoration-7>
          </div>
          <div class="filter-container">
              <el-input v-model="listQuery.searchword" :placeholder="'请输入关键字'" clearable style="width: 200px;margin-right: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />

              <el-select v-model="listQuery.order" style="width: 140px;margin-right: 10px;" class="filter-item" @change="handleFilter">
                <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />
              </el-select>

              <el-button v-waves  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                {{ '搜索' }}
              </el-button>
              <el-button v-waves :disabled="!checkPermission(['admin.create'])" class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
                {{ '添加' }}
              </el-button>
          </div>
          <el-table
              
              :header-cell-style="{background:'#eef1f6',color:'#606266'}"
              :data="list"
              size="medium"
              fit
              class="table-container"
              highlight-current-row
              style="width: 100%"
            >
              <el-table-column  :label="'账号'">
                <template slot-scope="scope">
                  <span>{{ scope.row.name }}</span>
                </template>
              </el-table-column>

              <el-table-column  :label="'昵称'">
                <template slot-scope="{row}">
                  <span>{{ row.nickname }}</span>
                </template>
              </el-table-column>

              <el-table-column  align="center" :label="'授权'">
                <template slot-scope="scope">
                  <el-button :disabled="!checkPermission(['admin.access'])" v-if="scope.row.is_root == 0" type="warning" size="mini" @click="handleAccess(scope.$index, scope.row)">
                    授权
                  </el-button>
                </template>
              </el-table-column>

              <el-table-column width="160px" align="center" :label="'添加时间'">
                <template slot-scope="scope">
                  <span>{{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
                </template>
              </el-table-column>

              <el-table-column align="center" :label="'操作'" width="350">
                <template slot-scope="scope">
                  <el-button :disabled="!checkPermission(['admin.update'])" type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                    {{ '编辑' }}
                  </el-button>
                  <el-button :disabled="!checkPermission(['admin.detail'])" type="info" size="mini" icon="el-icon-info" @click="handleDetail(scope.$index, scope.row)">
                    {{ '详情' }}
                  </el-button>
                  <el-button :disabled="!checkPermission(['admin.modifyPassword'])" type="warning" size="mini" @click="handlePassword(scope.$index, scope.row)">
                    {{ '改密' }}
                  </el-button>
                  <el-button  v-if="scope.row.is_root == 0" v-permission="['admin.delete']" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                    {{ '删除' }}
                  </el-button>
                </template>
              </el-table-column>
          </el-table>
          <dv-decoration-10 :dur="1" style="height:5px;" />
          <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
        </el-card>
      </div>
    </div>

   

    <el-dialog   width="650px"  :visible.sync="detail.dialogVisible" :show-close="false">
      <dv-border-box-11  class="el-new-dialog" title="账号详情">
        <detail :data="detail.data" />
      </dv-border-box-11>
    </el-dialog>

    <el-dialog  width="650px" :visible.sync="create.dialogVisible"  :show-close="false" @close="getList">
      <dv-border-box-11  class="el-new-dialog" title="添加账号">
        <create :item="create" />
      </dv-border-box-11>
    </el-dialog>

    <el-dialog  width="650px"  :visible.sync="edit.dialogVisible"  :show-close="false"  @close="getList">
      <dv-border-box-11  class="el-new-dialog" title="编辑账号">
        <edit :item="edit" />
      </dv-border-box-11>
    </el-dialog>

    <el-dialog  width="650px"    :show-close="false" :visible.sync="password.dialogVisible">
      <dv-border-box-11  class="el-new-dialog" title="修改密码">
        <el-form>
          <el-form-item :label="'新密码'">
            <el-input v-model="password.newpassword" type="password" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="changePassword">{{ '确认' }}</el-button>
          </el-form-item>
        </el-form>
      </dv-border-box-11>
    </el-dialog>

    <el-dialog   width="650px"    :show-close="false"   :visible.sync="access.dialogVisible">
      <dv-border-box-11  class="el-new-dialog" title="账号授权">
        <access :item="access" />
      </dv-border-box-11>
    </el-dialog>
  </div>
</template>

<script>
import md5 from 'js-md5'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
import checkPermission from '@/utils/permission' // 权限判断函数
import Detail from '@/components/Maidou/Detail'
import Edit from './components/Edit'
import Create from './components/Create'
import Access from './components/Access'
import { list, detail, del, modifyPassword } from '@/api/admin'
import { parseTime } from '@/utils'
export default {
  name: 'AdminIndex',
  components: { Pagination, Detail, Edit, Create, Access },
  directives: { waves, permission },
  filters: {
    parseTime
  },
  data() {
    return {
      list: null,
      total: 0,
      // listLoading: true,
      listQuery: {
        searchword: '',
        order: 'DESC',
        status: '',
        page: 1,
        limit: 10
      },
      sortOptions: [
        { key: 'ASC', label: '正序' },
        { key: 'DESC', label: '倒叙' }
      ],
      create: {
        dialogVisible: false
      },
      edit: {
        dialogVisible: false,
        id: ''
      },
      detail: {
        dialogVisible: false,
        data: []
      },
      access: {
        id: '',
        name: '',
        dialogVisible: false
      },
      password: {
        dialogVisible: false,
        id: '',
        newpassword: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    getList() {
      // this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        // this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.create.dialogVisible = true
    },
    handleEdit(index, row) {
      this.edit.dialogVisible = true
      this.edit.id = row.id
    },
    handleAccess(index, row) {
      this.access.id = row.id
      this.access.name = row.name
      this.access.dialogVisible = true
    },
    handleDetail(index, row) {
      detail(row.id).then((res) => {
        this.detail.dialogVisible = true
        const data = res.data
        this.detail.data = [
          {
            name: 'ID',
            content: data.id,
            type: 'text'
          },
          {
            name: '账号',
            content: data.name,
            type: 'text'
          },
          {
            name: '昵称',
            content: data.nickname,
            type: 'text'
          },
          {
            name: '简介',
            content: data.introduce,
            type: 'text'
          },
          {
            name: '加入时间',
            content: data.create_time,
            type: 'time'
          },
          {
            name: '最近活动',
            content: data.last_active,
            type: 'time'
          },
          {
            name: '最近活动IP',
            content: data.last_ip,
            type: 'text'
          }
        ]
      })
    },
    handleDelete(index, row) {
      const thiz = this
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1500,
            onClose() {
              thiz.list.splice(index, 1)
            }
          })
        })
      }).catch(() => {

      })
    },
    handlePassword(index, row) {
      this.password.dialogVisible = true
      this.password.id = row.id
    },
    changePassword() {
      if (this.password.newpassword === '') {
        this.$message({
          message: '密码不能为空',
          type: 'error',
          duration: 1500
        })

        return false
      }

      modifyPassword({
        id: this.password.id,
        password: md5(this.password.newpassword)
      }).then(() => {
        const thiz = this
        this.$message({
          message: '密码修改成功',
          type: 'success',
          duration: 1500,
          onClose() {
            thiz.password.newpassword = ''
            thiz.password.dialogVisible = false
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.pagination-container {
  padding: 5px 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
